package br.edu.univas.si.engs.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import br.edu.univas.si.engs.controller.Controller;

import br.edu.univas.si.engs.model.CarTO;
import br.edu.univas.si.engs.model.ClientTO;
import br.edu.univas.si.engs.model.LocDAO;
import br.edu.univas.si.engs.model.RentTO;
import br.edu.univas.si.engs.model.ReserveTO;



public class PanelConsultarClienteLoc extends JPanel {
	
	
	private static final long serialVersionUID = 1L;

	private static final ClientTO clientTO = null;



	private static final ReserveTO reserveTO = null;

	private static final LocDAO locDAO = null;

	private static final RentTO rentTO = null;

	private static final CarTO carTO = null;
	
	private JLabel nomeLabel;
	private JTextField nomeFiled;
	
	public void setNomeFiled(String nomeFiledd) {
		System.out.println(nomeFiledd);
		this.nomeFiled.setText(nomeFiledd) ;
	}




	private GridBagConstraints nomeLabelConstraints;
	private GridBagConstraints nomeFildConstraints;
	
    private JTable table;
	private DefaultTableModel tableModel;
	private JScrollPane scroll;
	private GridBagConstraints tablecBagConstraints;

	
	private JButton consultarButton;
	private JButton limparButton;
	private ButtonListenerConsultarclienteLoc buttonListenerConsultaclienteLoc;
	
	
	
	private GridBagConstraints consultaBagConstraints;
	private GridBagConstraints limparBagConstraints;
	
	private Controller  controller;
	
	
	
	
	public Controller getController() {
		if(controller==null){
		
			controller=new Controller(clientTO, carTO, reserveTO, rentTO, locDAO);
		}
		return controller;
	}




	ArrayList<ButtonListenerConsultarclienteLoc > listenerLoc = new ArrayList<ButtonListenerConsultarclienteLoc>();
	
	
	
	
	public PanelConsultarClienteLoc(String cliente){
		
		setLayout(new GridBagLayout());
		setBorder(new EmptyBorder(6, 6, 6, 6));
		initialize(cliente);
		
	}
	

	public GridBagConstraints getConsultaBagConstraints() {
		consultaBagConstraints=createConstraintPrototype();
		consultaBagConstraints.gridx=2;
		consultaBagConstraints.gridy=0;
		return consultaBagConstraints;
	}

	public GridBagConstraints getLimparBagConstraints() {
		limparBagConstraints=createConstraintPrototype();
		limparBagConstraints.gridx=3;
		limparBagConstraints.gridy=0;
		return limparBagConstraints;
	}
	
	
	public void setButtonListenerConsultacliente(
			ButtonListenerConsultarclienteLoc buttonListenerConsultaclienteLoc) {
		this.listenerLoc.add( buttonListenerConsultaclienteLoc);
	}

	public JButton getConsultarButton() {
		if(consultarButton==null){
			consultarButton=new JButton();
			consultarButton.setText("Consultar");
			consultarButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
for (ButtonListenerConsultarclienteLoc	 list : listenerLoc) {
						
						if(list!=null){
							list.consultarAction();
						}
					}
					
				}
			});
		}
		return consultarButton;
	}
	/**
	 * @return the limparButton
	 */
	public JButton getLimparButton() {
		
		if(limparButton==null){
			limparButton=new JButton();
			limparButton.setText("Limpar");
			limparButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					for (ButtonListenerConsultarclienteLoc	 list : listenerLoc) {
						
						if(list!=null){
							list.limparAcition();
						}
					}
						
					
					
				}
			});
		
		
		}
		return limparButton;
	}
	


	private void initialize(String cliente) {
		
		add(getNomeLabel(),getNomeLabelConstraints());
		add(getNomeFiled(cliente),getNomeFildConstraints());
		add(getConsultarButton(),getConsultaBagConstraints());
		add(getLimparButton(),getLimparBagConstraints());
		add(getScroll(),getTablecBagConstraints());	
	}
	

	
	public JLabel getNomeLabel() {
		if(nomeLabel==null){
			nomeLabel= new JLabel();
			nomeLabel.setText("Nome");
		}
		return nomeLabel;
	}
	/**
	 * @param  
	 * @return the nomeFiled
	 */
	public JTextField getNomeFiled(String cliente) {
		System.out.println("Clientez:"+cliente);
		if(nomeFiled==null){
			nomeFiled= new JTextField();
			
			
		
			
			
		}
		return nomeFiled;
	}
	/**
	 * @return the nomeLabelConstraints
	 */
	
	private GridBagConstraints createConstraintPrototype() {
		GridBagConstraints gbc = new GridBagConstraints();
		int space = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(space, space, space, space);
		return gbc;
	}
	
	
	public GridBagConstraints getNomeLabelConstraints() {
		if(nomeLabelConstraints==null){
			nomeLabelConstraints=createConstraintPrototype();
			nomeLabelConstraints.gridx=0;
			nomeLabelConstraints.gridy=0;
			
		}
		
		return nomeLabelConstraints;
	}
	/**
	 * @return the nomeFildConstraints
	 */
	public GridBagConstraints getNomeFildConstraints() {
		if(nomeFildConstraints==null){
			nomeFildConstraints=createConstraintPrototype();
			nomeFildConstraints.gridx=1;
			nomeFildConstraints.gridy=0;
			nomeFildConstraints.ipadx=550;
			
		}
		return nomeFildConstraints;
	}
	
	
	

	//Borda
	
	private DefaultTableModel getTableModel() {
		if(tableModel == null){
			tableModel = new DefaultTableModel();
			//tableModel.fireTableStructureChanged();
			tableModel.setColumnIdentifiers(new String[]{
				"Codigo","Nome","CPF","RG","Telefone","Celular","Estado","Endereco","Cidade","Status"
			});
		}
		
		
		
		
		
		return tableModel;
	}
	
	

	
	
	private JTable getTable() {
		
			if(table==null){
				table= new JTable();
				
				
				 
				table.setModel(getTableModel());
				table.addMouseListener (new MouseAdapter (){ 
					public void mouseReleased (MouseEvent e) 
					{ 
						if (e.getClickCount () == 2) 
						{ 
							
								int idCliente= Integer.parseInt( (String) table.getValueAt(table.rowAtPoint (e.getPoint ()),0));
								String nomeCliente=(String) table.getValueAt(table.rowAtPoint (e.getPoint ()),1);
								JOptionPane.showMessageDialog(null	, "Cliente escolhido");
								
									
								
								
								FrameConsultarCarroLoc frameConsultarCarroLoc=new FrameConsultarCarroLoc(null, nomeCliente, idCliente, null, null, true);
								frameConsultarCarroLoc.setVisible(true);
								LocarJFrame.getIdeCliente(idCliente,nomeCliente);
							    
									
								  
			                         
								}
								
								
						
					}
					 
				}); 
				

				
			}
		
		return table;
	
	}
	
	private JScrollPane getScroll() {
		if(scroll == null){
			scroll = new JScrollPane(getTable());
			scroll.setPreferredSize(new Dimension(0, 0));
			
		
			
		}
		return scroll;
	}
	
	public GridBagConstraints getTablecBagConstraints() {
		tablecBagConstraints = createConstraintPrototype();
		tablecBagConstraints.gridx = 0;
		tablecBagConstraints.gridy = 1;
		tablecBagConstraints.gridwidth = 4;
		tablecBagConstraints.ipady=400;
		return tablecBagConstraints;
	}
	
	
	
	
	
	public void addContact(ArrayList<ClientTO> novoArray ) {
		
	for (ClientTO clientesTo : novoArray) {
		 
		String[] line= new String[]{
			
				Integer.toString(clientesTo.getId()),
				clientesTo.getNome(),
				clientesTo.getCpf(),
				clientesTo.getRg(),
				clientesTo.getTelefone(),
				clientesTo.getCelular(),
				
				};
		getTableModel().addRow(line);
		
	}
		
}
	
	

	
	
	
	
	
	public void limparTable(){
		tableModel.setNumRows(0);  
	}


	public ClientTO getcontactClienteTo(){
		ClientTO c = new ClientTO();
		c.setNome(getNomeFiled(null).getText());
		return c;
	
	}


	public void getNomeFiled1(String text) {
		nomeFiled.setText(text);
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	

}
